package com.flyduck.mall.pms.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyduck.mall.pms.api.constant.ProductConstant;
import com.flyduck.mall.pms.domain.PmsProduct;
import com.flyduck.mall.pms.domain.PmsSkuStock;
import com.flyduck.mall.pms.domain.PmsSpecificationValue;
import com.flyduck.mall.pms.h5.service.H5ProductService;
import com.flyduck.mall.pms.h5.vo.H5ProductDetailsRespVO;
import com.flyduck.mall.pms.h5.vo.H5ProductQueryReqVO;
import com.flyduck.mall.pms.h5.vo.H5ProductRespVO;
import com.flyduck.mall.pms.manager.PmsSkuStockManager;
import com.flyduck.mall.pms.manager.PmsSpecificationManager;
import com.flyduck.mall.pms.mapper.PmsCategoryMapper;
import com.flyduck.mall.pms.mapper.PmsProductMapper;

import com.flyduck.mall.common.core.utils.BeanUtils;
import com.flyduck.mall.common.mybatis.utils.PageUtils;
import com.flyduck.ss.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-05-18 21:51
 **/
@Service
public class H5ProductServiceImpl implements H5ProductService {

    @Resource
    private PmsCategoryMapper pmsCategoryMapper;
    @Resource
    private PmsProductMapper pmsProductMapper;
    @Resource
    private PmsSpecificationManager pmsSpecificationManager;
    @Resource
    private PmsSkuStockManager pmsSkuStockManager;

    @Override
    public Page<H5ProductRespVO> getProductPage(Page page, H5ProductQueryReqVO h5ProductQueryReqVO) {
        Page productPage = pmsProductMapper.selectPage(page,
                new LambdaQueryWrapper<PmsProduct>()
                        .eq(PmsProduct::getIsOnSale, true)
                        .eq(PmsProduct::getCategoryId, h5ProductQueryReqVO.getCategoryId())
                        .orderByDesc(ProductConstant.AppProductSort.DEFAULT_SORT.getCode() == h5ProductQueryReqVO.getSort(), PmsProduct::getIsTop, PmsProduct::getSortOrder)//0:默认排序
                        .orderByDesc(ProductConstant.AppProductSort.NEWEST_FIRST.getCode() == h5ProductQueryReqVO.getSort(), PmsProduct::getCreateTime)//1:按照上市时间
                        .orderByDesc(ProductConstant.AppProductSort.HIGHEST_SALES.getCode() == h5ProductQueryReqVO.getSort(), PmsProduct::getSale)//2:销量
        );

        return PageUtils.convert(productPage, H5ProductRespVO.class);
    }

    @Override
    public H5ProductDetailsRespVO getProductDetailsById(Long productId) {
        // 商品详情
        PmsProduct pmsProduct = pmsProductMapper.selectById(productId);
        if (pmsProduct == null) {
            throw new ServiceException("商品不存在");
        }
        if (!pmsProduct.getIsOnSale()) {
            throw new ServiceException("商品未上架");
        }

        H5ProductDetailsRespVO h5ProductDetailsRespVO = BeanUtils.toBean(pmsProduct, H5ProductDetailsRespVO.class);

        // 商品属性
        List<PmsSpecificationValue> specificationValueList = pmsSpecificationManager.getAppSpecificationValueListByProductId(productId);
        List<H5ProductDetailsRespVO.PmsSpecificationValueVO> appSpecificationValueVOS = BeanUtils.toBeanList(specificationValueList, H5ProductDetailsRespVO.PmsSpecificationValueVO.class);
        h5ProductDetailsRespVO.setSpecifications(appSpecificationValueVOS);

        // 商品可选sku及库存
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockManager.getAppSkuStockListByProductId(productId);
        List<H5ProductDetailsRespVO.PmsSkuStockVO> appSkuStockVOS = BeanUtils.toBeanList(pmsSkuStockList, H5ProductDetailsRespVO.PmsSkuStockVO.class);
        h5ProductDetailsRespVO.setSkuStocks(appSkuStockVOS);

        return h5ProductDetailsRespVO;
    }
}
